package com.teleca.jamendo.dialog;

import org.json.JSONException;

import android.app.Activity;
import android.content.Intent;

import com.teleca.jamendo.JamendoApplication;
import com.teleca.jamendo.api.JamendoGet2Api;
import com.teleca.jamendo.api.Playlist;
import com.teleca.jamendo.api.WSError;
import com.teleca.jamendo.api.impl.JamendoGet2ApiImpl;
import com.teleca.jamendo.mode.Album;
import com.teleca.jamendo.mode.Track;
import com.teleca.jamendo.ui.activity.PlayerActivity;

public class PlayerAlbumLoadingDialog extends LoadingDialog<Album, Track[]> {

	private Album mAlbum;

	public PlayerAlbumLoadingDialog(Activity activity, int loadingMsg, int failMsg) {
		super(activity, loadingMsg, failMsg);
	}

	@Override
	public Track[] doInBackground(Album... params) {
		mAlbum = params[0];

		JamendoGet2Api service = new JamendoGet2ApiImpl();
		Track[] tracks = null;

		try {
			tracks = service.getAlbumTracks(mAlbum, JamendoApplication.getInstance().getStreamEncoding());
		} catch (JSONException e) {
			e.printStackTrace();
			return null;
		} catch (WSError e) {
			publishProgress(e);
			cancel(true);
		}
		return tracks;

	}

	@Override
	public void doStuffWithResult(Track[] tracks) {

		Intent intent = new Intent(mActivity, PlayerActivity.class);
		Playlist playlist = new Playlist();
		mAlbum.setTracks(tracks);
		playlist.addTracks(mAlbum);

		intent.putExtra("playlist", playlist);
		mActivity.startActivity(intent);
	}

}
